<template>
	<view class="login">
		<view class="login_image">
		</view>
		<view class="login_info">
			<view class="login_phone">
				手机号<input placeholder="请输入手机号" type="text" v-model="phone" />
			</view>
			<view class="login_phone">
				验证码<input placeholder="请输入验证码" type="text" v-model="password" />
				<view class="u-demo-block">
					<view class="u-demo-block__content">
						<u-code ref="uCode2" @change="codeChange2" keep-running start-text="点我获取验证码"></u-code>
						<text @tap="getCode2" :text="tips2" class="u-page__code-text">{{tips2}}</text>
					</view>
				</view>
			</view>
		</view>
		<view class="Getbutton">
			<u-button shape="circle" type="success" text="确定" @click="GoPhone"></u-button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				phone: '',
				password: '',
				tips2: ''
			}
		},
		onLoad() {
			console.log()
		},
		methods: {
			changeCheck() {
				this.checked = !this.checked;
			},
			codeChange2(text) {
				this.tips2 = text;
			},
			GoPhone() {
				// console.log()
				// let openId
				// uni.getStorage({
				// 	key: 'openId',
				// 	success: function (res) {
				// 		openId = res.data
				// 	}
				// });
				if (this.phone === '') {
					callback1(new Error('手机号不能为空'))
				} else {
					var reg =
						/^1(3\d{2}|4[14-9]\d|5([0-35689]\d|7[1-79])|66\d|7[2-35-8]\d|8\d{2}|9[13589]\d)\d{7}$/;

					if (!reg.test(this.phone)) {
						callback(new Error('请输入有效的手机号码'));
					} else {
						this.$http({
							url: '/MemberLogin/acquireWxCDKMemberInfo',
							data: {
								openId:uni.getStorageSync('openId'),
								memberName: this.phone,
								verificationCode: this.password
							}
						}).then(res => {
							console.log(res)
							if(res.data.code === 200){
								uni.switchTab({
									url:'../cart/cart'
								})
							}
						})
					}
				}

				function callback1(Error) {
					uni.showModal({
						title: '手机号码不能为空',
					});
				}

				function callback(Error) {
					uni.showModal({
						title: '请输入有效的手机号码',
					});

				}
			},
			getCode2() {
				if (this.phone === '') {
					callback1(new Error('手机号不能为空'))
				} else if (this.phone != '') {
					var reg =
						/^1(3\d{2}|4[14-9]\d|5([0-35689]\d|7[1-79])|66\d|7[2-35-8]\d|8\d{2}|9[13589]\d)\d{7}$/;

					if (!reg.test(this.phone)) {
						callback(new Error('请输入有效的手机号码'));

					} else {
						if (this.phone) {
							// 模拟向后端请求验证码
							uni.showLoading({
								title: '正在获取验证码'
							})
							this.$http({
								url: "/MemberLogin/sendSms",
								data: {
									templateId: '1333541',
									phoneNumberSet: this.phone
								}
							}).then(res => {
								if (res.data.code != 200) {
									uni.hideLoading();
									uni.showToast({
										title: res.data.message
									})
									return
								} else {
									setTimeout(() => {
										uni.hideLoading();
										uni.$u.toast('验证码已发送');
										this.$refs.uCode2.start();
									}, 2000);
								}
							})

						} else {
							uni.$u.toast('请等待时间季结束');
						}
					}

				}

				function callback1(Error) {
					uni.showModal({
						title: '手机号码不能为空',
					});
				}

				function callback(Error) {
					uni.showModal({
						title: '请输入有效的手机号码',
					});

				}
			}
		}
	}
</script>

<style lang="scss">
	@import './login.css';
</style>
